﻿/**
* @file seven_seg_led.c
* @brief this is a brief description
* @details this is the detail description.
* @author fulong 
* @data 2014/10/10
* @version 1.0
* @par Copyright (c):
* @par First build
*     version: fulong 
*     data: 2014/10/10
*/
#include "seven_seg_led.h"
#include "../../bsp/bsp.h"
#include "../io_configure.h"
#ifdef __R8C
#include "../uart.h"
#endif
#ifdef __STM32
    #include "../../bsp/arch/stm32/stm32f1xx/STM32F10x_StdPeriph_Driver/inc/stm32f10x_gpio.h"
#endif
#ifdef __SIMPLE_DISPLAY__


/********************************************************************
              A
           _________
        /            /
   F  /   G       /	B
     /--------- /
E /	       / C
 /_________ /
D		. H
********************************************************************/
#define	CHAR_0			(~(LED_A + LED_B + LED_C + LED_D + LED_E + LED_F ))
#define	CHAR_1			(~(LED_B + LED_C ))
#define	CHAR_2			(~(LED_A + LED_B + LED_D + LED_E + LED_G))
#define	CHAR_3			(~(LED_A + LED_B + LED_C + LED_D + LED_G))
#define	CHAR_4			(~(LED_B + LED_C + LED_F + LED_G))
#define	CHAR_5			(~(LED_A + LED_C + LED_D + LED_F + LED_G))
#define	CHAR_6			(~(LED_A + LED_C + LED_D + LED_E + LED_F + LED_G))
#define	CHAR_7			(~(LED_A + LED_B + LED_C))
#define	CHAR_8			(~(LED_A + LED_B + LED_C + LED_D + LED_E + LED_F + LED_G))
#define	CHAR_9			(~(LED_A + LED_B + LED_C + LED_D + LED_F + LED_G))
#define	CHAR_A			(~(LED_A + LED_B + LED_C + LED_E + LED_F + LED_G))
#define	CHAR_B			(~(LED_C + LED_D + LED_E + LED_F + LED_G))
#define	CHAR_C			(~(LED_A + LED_D + LED_E + LED_F))
#define	CHAR_D			(~(LED_B + LED_C + LED_D + LED_E + LED_G))
#define	CHAR_E			(~(LED_A + LED_D + LED_E + LED_F + LED_G))
#define	CHAR_F			(~(LED_A + LED_E + LED_F + LED_G))
#define	CHAR_P			(~(LED_A + LED_B + LED_E + LED_F + LED_G))
#define	CHAR_N			(~(LED_G))
#define	CHAR_NULL	     (0xFF)

#define FONT_MAX_LEN 38

const uint8 seven_seg_font[FONT_MAX_LEN] = { '0', (uint8)CHAR_0, '1', (uint8)CHAR_1, '2', (uint8)CHAR_2, '3', (uint8)CHAR_3, '4', (uint8)CHAR_4, '5', (uint8)CHAR_5, '6', (uint8)CHAR_6,
    '7', (uint8)CHAR_7, '8', (uint8)CHAR_8, '9', (uint8)CHAR_9, 'A', (uint8)CHAR_A, 'B', (uint8)CHAR_B, 'C', (uint8)CHAR_C, 'D', (uint8)CHAR_D, 'E', (uint8)CHAR_E, 'F', (uint8)CHAR_F, 'P', (uint8)CHAR_P, '-', (uint8)CHAR_N, 0, 0xFF };

extern uint8 get_seven_seg_index(uint8 ascii);

void seven_seg_led_init(void)
{
#if LED_SEG_ROW >0
#if LED_SEG_LEN > 0
    set_led_seg1_output()
    LED_B1(Bit_SET);
#endif
#if LED_SEG_LEN > 1
    set_led_seg2_output()
    LED_B2(Bit_SET);
#endif
#if LED_SEG_LEN > 2
    set_led_seg3_output()
    LED_B3(Bit_SET);
#endif
    set_led_data_port_output();
#endif
#if LED_SEG_ROW > 1
#if LED_SEG_LEN1 > 0
    set_led_seg1_output1()
    LED1_B1(Bit_SET);
#endif
#if LED_SEG_LEN1 > 1
    set_led_seg2_output1()
    LED1_B2(Bit_SET);
#endif
#if LED_SEG_LEN1 > 2
    set_led_seg3_output1()
    LED1_B3(Bit_SET);
#endif
    set_led_data_port_output1();
#endif
}
void seven_seg_display(uint8 x, const uint8 *ascii, const uint8 location)
{
    uint8 seven_seg_index;
#if LED_SEG_ROW >0
#if LED_SEG_LEN > 0
    LED_B1((BitAction)(((1 << x) & 0xFE) && 1));
#endif
#if LED_SEG_LEN > 1
    LED_B2((BitAction)(((1 << x) & 0xFD) && 1));
#endif
#if LED_SEG_LEN > 2
    LED_B3((BitAction)(((1 << x) & 0xFB) && 1));
#endif
    if (location != 0xFF)
    {
        seven_seg_index = get_seven_seg_index(*(ascii + location % LED_SEG_LEN)) + 1;
    } else
    {
        seven_seg_index = get_seven_seg_index(0) + 1;
    }
    LEDDATAPORT(seven_seg_font[seven_seg_index]);
#endif
#if LED_SEG_ROW >1
#if LED_SEG_LEN1 > 0
    LED1_B1((BitAction)(((1 << x) & 0xFE) && 1));
#endif
#if LED_SEG_LEN1 > 1
    LED1_B2((BitAction)(((1 << x) & 0xFD) && 1));
#endif
#if LED_SEG_LEN1 > 2
    LED1_B3((BitAction)(((1 << x) & 0xFB) && 1));
#endif
    if (location != 0xFF)
    {
        seven_seg_index = get_seven_seg_index(*(ascii + LED_SEG_LEN + location % LED_SEG_LEN1)) + 1;
    } else
    {
        seven_seg_index = get_seven_seg_index(0) + 1;
    }
    LEDDATAPORT1(seven_seg_font[seven_seg_index]);
#endif
}
uint8 get_seven_seg_index(uint8 ascii)
{
    uint8 i = 0;
    while (ascii != seven_seg_font[i] && i < FONT_MAX_LEN)
    {
        i += 2;
    }
    return (i);
}
#endif
